package com.andy.qhb.controller;

import java.util.Collections;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.andy.qhb.base.PageUtil;
import com.andy.qhb.base.PageVO;
import com.andy.qhb.base.RespVO;
import com.andy.qhb.base.table.PageStructure;
import com.andy.qhb.base.table.Search;
import com.andy.qhb.base.table.TableHeader;
import com.andy.qhb.dao.DaoUtils;
import com.andy.qhb.dao.vo.Where;
import com.andy.qhb.entity.UsrFabLog;

@Controller
@RequestMapping("admin/fabLog")
public class UsrFabLogController extends BaseController{

	/**
	 * 用户发包记录列表：结构
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="list")
	public ModelAndView list() throws Exception {
		TableHeader tableHeader = new TableHeader();
		tableHeader.setNames(new String[]{"uid", "fname", "coin:yuan", "num","room_id","create_time:datetime"});
		tableHeader.setTexts(new String[]{"玩家ID", "玩家名称","红包金币","包数","房间号","发包时间"});
		
		Search search = new Search();
		search.setNames(new String[]{"uid"});
		search.setTexts(new String[]{"玩家ID"});
		search.setTypes(new String[]{"text"});
		
		PageStructure data = PageUtil.createTablePageStructure("admin/fabLog/listData", "id", tableHeader,null,search);
		return createMV("tableList","发包记录", Collections.singletonMap("formStruct", data));
	}
	
	/**
	 * 用户发包记录：数据
	 * @param page
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="listData")
	public RespVO listData(Integer page,Integer limit,String uid) throws Exception {
		PageVO logs = null;
		if(StringUtils.isNotBlank(uid)){
			logs = DaoUtils.getPageForMap(UsrFabLog.table(),page,limit,Where.eq("uid", uid));
		}else{
			logs = DaoUtils.getPageForMap(UsrFabLog.table(),page,limit);
		}
//		for(Map<String, Object> item:logs.getItems()){
//			if(item.get("nick_name") == null){
//				item.put("nick_name", "机器人");
//			}
//		}
        return RespVO.createSuccessJsonResonse(logs);
	}
	
	
}
